package com.efast.cafe.portal.bean.query;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


import com.efast.cafe.framework.util.QueryStringUtil;
import com.efast.cafe.framework.util.SqlConstants.FieldType;
import com.efast.cafe.framework.util.SqlConstants.OpType;

public class TimeStampFieldType extends AbstractFieldType {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2921439629914131861L;
	private static Integer count = 0;
	private static Boolean isDate = false;
	@Override
	public <T extends CommonTableConditionBean> T getHandledObject(QueryParameterBean p,T t) {
		count = 0;
		String value1="";
		String value2="";
		String[] array = t.getValue().split("~");
		if (array.length > 1) {
			value1 = array[0];
			value2 = array[1];
			handleBetweenData(p,t, value1, value2);
		} else {
			
			return handleCommonData(p,t,false);
		}
		return t;
	}
	
	/**
	 * 处理单个时间类型
	 * @param t
	 * @return
	 */
	public <T extends CommonTableConditionBean> T handleCommonData(QueryParameterBean p,T t,boolean isBetween) {
		try {
			SimpleDateFormat simpleDateFormat =  null;
			String key = t.getKey();
			Integer counts = p.getIndex() + count;
			String sqlIndex = QueryStringUtil.getSqlDefaultValue(counts.toString());
			String value = t.getValue();
			t.setOpType(OpType.getOp(t.getOp()));
			Integer fieldType = t.getFiledType();
			if (FieldType.Date.equals(fieldType)) {
				isDate = true;
				t.setFiledType(FieldType.DateTime);
				if (OpType.eq.equals(t.getOp()) || OpType.not_eq.equals(t.getOp()) || OpType.today.equals(t.getOp())) {
					simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
					if (OpType.today.equals(t.getOp())) {
						value = simpleDateFormat.format(new Date());
					}
					value = parseDateToDateTime(value);
					if (!OpType.not_eq.equals(t.getOp())) {
						t.setOp(OpType.between_not);
					} 
					String value1 =parseDateToDateTime(value, 1);
					t.setValue(value+"~"+value1);
					return getHandledObject(p,t);
				} else if (OpType.gt.equals(t.getOp()) || OpType.le.equals(t.getOp())) {
					t.setValue(parseDateToDateTime(value,1));
					return handleCommonData(p,t, isBetween);
				} else {
					t.setValue(parseDateToDateTime(value,0));
					return handleCommonData(p,t, isBetween);
				}
				
			} else if (FieldType.DateTime.equals(fieldType)) {
				simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				t.setValue(QueryStringUtil.changeToDateTime(sqlIndex));
				if (!isDate &&(OpType.gt.equals(t.getOp())) || OpType.le.equals(t.getOp())) {
					t.setRealValue(addTime(value, fieldType, 1));
				} else {
					t.setRealValue(value);
				}
				
				if (OpType.today.equals(t.getOp())) {
					t.setRealValue(simpleDateFormat.format(new Date()));
				}
			} else if (FieldType.Time.equals(fieldType)) {
				simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
				t.setValue(QueryStringUtil.changeToTime(sqlIndex));
				if (OpType.gt.equals(t.getOp()) || OpType.le.equals(t.getOp())) {
					t.setRealValue(addTime(value, fieldType, 1));
				} else {
					t.setRealValue(value);
				}
				if (OpType.today.equals(t.getOp())) {
					
				}
			}
			if (OpType.today.equals(t.getOp())) {
				t.setOpType(OpType.getOp(OpType.eq));
			} 
			
			if (!isBetween) {
				t.setCombineSql(contactSql(p,t,key));
			} else {
				count ++;
			}
		} catch (Exception e){
			e.printStackTrace();
		}
		
		return t;
	}
	/**
	 * 处理时间范围
	 * @param t
	 * @param value1 （小）
	 * @param value2 （大）
	 * @return
	 */
	public <T extends CommonTableConditionBean> T handleBetweenData(QueryParameterBean p,T t,String value1,String value2) {
		String combineSql = "";
		CommonTableConditionBean bean1 = new CommonTableConditionBean();
		CommonTableConditionBean bean2 = new CommonTableConditionBean();
		String op = " AND ";
		if (isDate && OpType.not_eq.equals(t.getOp())) {
			op = " OR ";
		}
		if (OpType.between_in.equals(t.getOp())) {
			bean2.setOp(OpType.le);
			bean1.setOp(OpType.ge);
		} else {
			bean2.setOp(OpType.lt);
			bean1.setOp(OpType.gt);
		}
		bean1.setFiledType(t.getFiledType());
		bean1.setKey(t.getKey());
		bean1.setValue(value1);
		bean2.setKey(t.getKey());
		bean2.setValue(value2);
		bean2.setFiledType(t.getFiledType());
		handleCommonData(p,bean1,true);
		handleCommonData(p,bean2,true);
		combineSql = contactSql(p,bean1,bean1.getKey()) +op+contactSql(p,bean2,bean2.getKey());
		t.setCombineSql(combineSql);
		return t;
	}
	private String parseDateToDateTime(String value1) {
		return parseDateToDateTime(value1, 0);
		
	}
	private String parseDateToDateTime(String value1,Integer days) {
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(simpleDateFormat.parse(value1));
			calendar.add(Calendar.DATE, days);
			return simpleDateFormat1.format(calendar.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return value1;
	}
	
	private static String addTime(String value1,Integer fieldType,Integer seconds) {
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("HH:mm:ss");
			if (FieldType.Time.equals(fieldType)) {
				value1 = simpleDateFormat.format(new Date()) + " "+value1;
			} else {
				simpleDateFormat2 = simpleDateFormat1;
			}
					
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(simpleDateFormat1.parse(value1));
			calendar.add(Calendar.SECOND, seconds);
			return simpleDateFormat2.format(calendar.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return value1;
	}
	
	
}
